#include <utility>
#include <cassert>
#include "testlib.h"

const int MAXVAL = 1000;
const int MAXEXPLEN = 255;

enum TokType {EOT, SIGN, NUMBER, BRACKET};

struct Token {
  TokType type;
  int value;
  char ch;

  Token (int value)
    : type(NUMBER)
    , value(value)
    , ch(0)
    {
    }

  Token(TokType type, char ch)
   : type(type)
   , value(0)
   , ch(ch)
   {}

};

char *cur;
int N;

void error() {
  quitf(_wa, "Syntax error in expression");
}

Token nextToken() {
  while (*cur == ' ' || *cur == '\t') {
    cur++;
  }

  if (*cur == 0) {
    return Token(EOT, 0);
  }

  if (*cur == '+' || *cur == '-') {
    char sign = *cur;
    cur++;
    while (*cur == '+' || *cur == '-' || *cur == ' ' || *cur == '\t') {
      if (*cur == '-') {
        sign = (sign == '+') ? '-' : '+';
      }
      cur++;
    }
    return Token(SIGN, sign);
  }

  if (*cur == '*' || *cur == '/' || *cur == '%') {
    return Token(SIGN, *(cur++));
  }

  if (*cur >= '0' && *cur <= '9') {
    return Token(strtol(cur, &cur, 10));
  }

  if (*cur == '(' || *cur == ')') {
    return Token(BRACKET, *(cur++));
  }

  if ((*cur == 'N' || *cur == 'n')) {
    cur++;
    return Token(N);
  }

  error();
  return Token(EOT, 0); // to supress warning
}

int expression();

int multiplier() {
  Token t = nextToken();
  int sign = 1;
  if (t.type == SIGN && (t.ch == '+' || t.ch == '-')) {
    sign = (t.ch == '-') ? -1 : 1;
    t = nextToken();
  }
  if (t.type == NUMBER) {
    return t.value * sign;
  }
  if (t.type == BRACKET && t.ch == '(') {
    int value = expression();
    t = nextToken();
    if (t.type == BRACKET && t.ch == ')') {
      return sign * value;
    }
    error();
  }
  error();
  return 0; // to supress warning
}

int addend() {
  int value = multiplier();
  for(;;) {
    while (*cur == ' ' || *cur == 9) {
      cur++;
    }
    if (*cur == '*') {
      cur++;
      value *= multiplier();
    } else if (*cur == '/') {
      cur++;
      value /= multiplier();
    } else if (*cur == '%') {
      cur++;
      value %= multiplier();
    } else {
      break;
    }
  }
  return value;
}

int expression() {
  int value = addend();
  for(;;) {
    while (*cur == ' ' || *cur == 9) {
      cur++;
    }
    if (*cur == '+') {
      cur++;
      value += addend();
    } else if (*cur == '-') {
      cur++;
      value -= addend();
    } else {
      break;
    }
  }
  return value;
}


int main( int argc, char *argv[] ) {

  registerTestlibCmd(argc, argv);

  N = inf.readInt();
  int rightAnswer = N * (N + 1) / 2;

  std::string studAnswerStr = ouf.readString();
  for (char *c = (char*)studAnswerStr.c_str(); *c != 0; c++) {
    if (*c == '–') {
      *c = '-';
    }
    if (*c == 'â' && *(c+1) == 'ˆ' && *(c+2) == '’') {
        *c = ' ';
        c++;
        *c = ' ';
        c++;
        *c = '-';
    } else   
    if (*c == 'ï' && *(c+1) == '»' && *(c+2) == '¿') {
        *c = ' ';
        c++;
        *c = ' ';
        c++;
        *c = ' ';
    } else
    if (*c=='&' && *(c+1)=='#' && *(c+2)=='8' && *(c+3)=='7' && *(c+4)=='2' && *(c+5)=='2' && *(c+6)==';') {
      *c = *(c+1) = *(c+2) = *(c+3) = *(c+4) = *(c+5) = *(c+6) = ' ';
      c += 6;
      *c = '-';
    }       
  }
  if(studAnswerStr.size() > MAXEXPLEN) {
    printf("Answer is too long ");
    quitf(_pe,"Answer is too long");    
  }
  cur = &studAnswerStr[0];
  int studAnswer = expression();
  if (studAnswer != rightAnswer) {
    printf("Expected %d, but found %d ", rightAnswer, studAnswer);
    quitf(_wa, "Expected %d, but found %d", rightAnswer, studAnswer);
  }
  if (!ouf.seekEof()) {
    printf("Extra information found ");
    quitf(_pe,"Extra information found");    
  }
  quitf(_ok, "Accepted");
  return 0;
}
